/*
 * Created by JFormDesigner on Thu Oct 13 15:35:51 CEST 2011
 */

package implementation1.gui;

import com.jgoodies.forms.factories.CC;
import com.jgoodies.forms.layout.FormLayout;
import exceptions.CreatingProcessException;
import implementation1.data.Account;
import implementation1.data.Handle;
import interfaces.app.IProcess;
import interfaces.gui.IMainWindow;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ResourceBundle;

/**
 * @author unknown
 */
public class MainWindow extends JFrame implements IMainWindow
{
    /**
     * reference to init process
     */
    private IProcess initProcess;

    /**
     * Default constructor.
     *
     * @param initProcess a reference to init process
     */
    public MainWindow(IProcess initProcess)
    {
        this.initProcess = initProcess;
        initComponents();
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        this.addWindowListener(new WindowAdapter()
        {
            /**
             * Invoked when a window has been closed.
             */
            @Override
            public void windowClosing(WindowEvent e)
            {
                if (!MainWindow.this.initProcess.quit())
                {
                    MainWindow.this.initProcess.terminate();
                }
            }
        });
    }

    private void button_signinActionPerformed(ActionEvent e)
    {
        try
        {
            IProcess shell = initProcess.createChildProcess(new Handle(null, null, null, null, "shell",
                    initProcess.getHandle().getActualPath(),
                    new Account(text_login.getText(), text_password.getPassword().toString(), Account.NORMAL_USER)));
            shell.startProcess();
        } catch (CreatingProcessException e1)
        {
            ResourceBundle bundle = ResourceBundle.getBundle("implementation1.res.messages");
            Message.showError(bundle.getString("error.ErrorWhileCreatingProcess"));
        }
    }

    private void text_loginKeyPressed(KeyEvent e)
    {
        text_passwordKeyPressed(e);
    }

    private void text_passwordKeyPressed(KeyEvent e)
    {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) button_signinActionPerformed(null);
    }

    private void initComponents()
    {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        ResourceBundle bundle = ResourceBundle.getBundle("implementation1.res.gui");
        label1 = new JLabel();
        text_login = new JTextField();
        label2 = new JLabel();
        text_password = new JPasswordField();
        button_signin = new JButton();

        //======== this ========
        setResizable(false);
        setTitle(bundle.getString("MainWindow.this.title"));
        Container contentPane = getContentPane();
        contentPane.setLayout(new FormLayout(
                "5dlu, $lcgap, 43dlu, $lcgap, 79dlu, $lcgap, 5dlu",
                "5dlu, 2*($lgap, default), $lgap, 3dlu, $lgap, default, $lgap, 5dlu"));

        //---- label1 ----
        label1.setText(bundle.getString("MainWindow.label1.text"));
        contentPane.add(label1, CC.xy(3, 3));

        //---- text_login ----
        text_login.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                text_loginKeyPressed(e);
            }
        });
        contentPane.add(text_login, CC.xy(5, 3));

        //---- label2 ----
        label2.setText(bundle.getString("MainWindow.label2.text"));
        contentPane.add(label2, CC.xy(3, 5));

        //---- text_password ----
        text_password.addKeyListener(new KeyAdapter()
        {
            @Override
            public void keyPressed(KeyEvent e)
            {
                text_passwordKeyPressed(e);
            }
        });
        contentPane.add(text_password, CC.xy(5, 5));

        //---- button_signin ----
        button_signin.setText(bundle.getString("MainWindow.button_signin.text"));
        button_signin.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                button_signinActionPerformed(e);
            }
        });
        contentPane.add(button_signin, CC.xywh(3, 9, 3, 1));
        setSize(255, 160);
        setLocationRelativeTo(null);
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField text_login;
    private JLabel label2;
    private JPasswordField text_password;
    private JButton button_signin;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
